import SComponents from '@/components/extends'
import { getCurrentInstance } from 'vue'
import * as Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.variable.min.css'
import '@/style/global.less'
import vuescroll from 'vuescroll'
import 'animate.css'
import store from '../store'
import { DELETE, GET, POST, PUT } from '../api/api'
import { TOGGLE_LOADING } from '../store/mutation-types'
import plugins from '@/plugins/index.js'
const prod = import.meta.env.PROD
import zh_CN from 'ant-design-vue/es/locale/zh_CN'
import dayjs from 'dayjs'
import dayJsLocal from 'dayjs/esm/locale/zh-cn'

async function bootstrap(inputApp) {
  dayjs.locale(dayJsLocal, null, true)
  const app = inputApp ? inputApp : getCurrentInstance().appContext.app
  app.use(SComponents)
  app.use(Antd)
  app.use(vuescroll)
  app.use(plugins)
  app.config.globalProperties.$loading = function (flag) {
    store.commit(TOGGLE_LOADING, flag)
  }
  app.config.globalProperties.$api = {
    GET: GET,
    POST: POST,
    DELETE: DELETE,
    PUT: PUT
  }
  app.config.globalProperties.CODES = {
    SUCCESS: '000000'
  }
  if (prod) {
    await appendStyle()
  }
}
async function appendStyle() {
  return new Promise((resolve, reject) => {
    const link = document.createElement('link')
    link.rel = 'stylesheet'
    link.href = '/lib/style.css'
    link.onload = resolve
    link.onerror = reject
    document.body.appendChild(link)
  })
}
export { bootstrap as bootstrap, zh_CN as locale }
